package practice;

import static org.junit.Assert.fail;

import org.junit.AfterClass;
import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.*;
import org.junit.BeforeClass;

import java.io.File;
import jxl.*; 


public class DataProviderExample {
    
	String Url = "http://www.imdb.com/";
	WebDriver driver = new ChromeDriver ();
	private StringBuffer verificationErrors = new StringBuffer();
    @BeforeClass
   

    
    @DataProvider(name = "DP1")
    public Object[][] createData1() throws Exception{
        Object[][] retObjArr=getTableArray("C:\\Users\\Alice\\Desktop\\eclipse\\samples\\data1.xls",
                "DataPool", "imdbTestData1");
        return(retObjArr);
    }
    
    @Test (dataProvider = "DP1")
    public void testDataProviderExample(String movieTitle, 
            String directorName, String moviePlot, String actorName) throws Exception {   
    	driver.get(Url);
        //enter the movie title 
        
        driver.findElement(By.name("q")).sendKeys(movieTitle);
       
        driver.findElement(By.xpath("/descendant::button[@type='submit']")).click();
        
        
        synchronized(driver){
        	driver.wait(5000);
        }
        
        //click on the movie title in the search result page
        
        driver.findElement(By.xpath("/descendant::a[text()='"+movieTitle+"']")).click();
        synchronized(driver){
        	driver.wait(5000);
        }
        //verify director name is present in the movie details page 
        
        Assert.assertTrue(driver.getPageSource().contains(directorName));
        
       
        //verify movie plot is present in the movie details page
        
        Assert.assertTrue(driver.getPageSource().contains(moviePlot));
        //verify movie actor name is present in the movie details page
      
        Assert.assertTrue(driver.getPageSource().contains(actorName));
    }
    
    @AfterClass
	public void tearDown() throws Exception {
		driver.quit();
		String verificationErrorString = verificationErrors.toString();
		if (!"".equals(verificationErrorString)) {
			fail(verificationErrorString);
		}
	}

	private boolean isElementPresent(By by) {
		try {
			driver.findElement(by);
			return true;
		} catch (NoSuchElementException e) {
			return false;
		}
	}
    
    public String[][] getTableArray(String xlFilePath, String sheetName, String tableName) throws Exception{
        String[][] tabArray=null;
        
            Workbook workbook = Workbook.getWorkbook(new File(xlFilePath));
            Sheet sheet = workbook.getSheet(sheetName); 
            int startRow,startCol, endRow, endCol,ci,cj;
            Cell tableStart=sheet.findCell(tableName);
            startRow=tableStart.getRow();
            startCol=tableStart.getColumn();

            Cell tableEnd= sheet.findCell(tableName, startCol+1,startRow+1, 100, 64000,  false);                

            endRow=tableEnd.getRow();
            endCol=tableEnd.getColumn();
            System.out.println("startRow="+startRow+", endRow="+endRow+", " +
                    "startCol="+startCol+", endCol="+endCol);
            tabArray=new String[endRow-startRow-1][endCol-startCol-1];
            ci=0;

            for (int i=startRow+1;i<endRow;i++,ci++){
                cj=0;
                for (int j=startCol+1;j<endCol;j++,cj++){
                    tabArray[ci][cj]=sheet.getCell(j,i).getContents();
                }
            }
        

        return(tabArray);
    }
    
    
}//end of class